#!/bin/bash

echo The zero parameter is set to: $0

name=$(basename $0)
echo The Script name is: $name

# 参数个数 $#
echo There were $# parameters supplied.

# 获取参数
# $0表示文件名 $1是第一个参数 以此类推
if [[ -n $1 ]]; then
	echo Hello $1, glad to meet you.
else
	echo "Sorry, you did not identify yourself. "
fi

# 获取所有参数
echo 所有参数：$@
echo 所有参数：$*

for para in $@; do
	echo "\$@>>"$para
done

for para in $*; do
	echo "\$*>>"$para
done


# echo
# echo "======"
# echo "shit [n] 移动变量"
# # 移动变量
# count=1
# while [ -n "$1" ]
# do
#    echo "Parameter #$count = $1"
#    count=$[ $count + 1 ]
#    shift
# done


# echo
# echo "======"
# echo "shit获取参数选项，！！！shift之后参数会被移走，不会再被取到"
# while [ -n "$1" ]
# do
#    case "$1" in
#      -a) echo "Found the -a option" ;;
#      -b) echo "Found the -b option" ;;
#      -c) echo "Found the -c option" ;;
#       *) echo "$1 is not an option" ;;
#    esac
# shift 
# done

# echo
# echo "======"
# echo "shit获取参数选项以及参数值"
# while [ -n "$1" ]
# do
#    case "$1" in
#       -a) echo "Found the -a option";;
#       -b) param="$2"
#           echo "Found the -b option, with parameter value $param"
#           shift ;;
#       -c) echo "Found the -c option";;
#       --) shift
#           break ;;
#        *) echo "$1 is not an option";;
# esac
# shift 
# done

echo
echo "======"
echo "getopt optstring parameters 获取参数选项以及参数值"
echo "optstring是这个过程的关键所在。它定义了命令行有效的选项字母，还定义了哪些选项字
母需要参数值。"
echo "getopt ab:cd -a -b test1 -cd test2 test3 "
echo ">>>>"
echo "-a -b test1 -c -d -- test2 test3"

echo
while getopts :ab:c opt
do
   case "$opt" in
      a) echo "Found the -a option" ;;
      b) echo "Found the -b option, with value $OPTARG";;
      c) echo "Found the -c option" ;;
      *) echo "Unknown option: $opt";;
esac 
done



